<template>
    <Form ref="formCom" class="xtx-form" :validation-schema="mySchema" autocomplete="off" v-slot="{errors}">
      <div class="xtx-form-item">
        <div class="field">
          <i class="icon iconfont icon-user"></i>
          <Field v-model="form.account" name="account" class="input" type="text" placeholder="请输入用户名" />
        </div>
        <div v-if="errors.account" class="error">{{ errors.account }}</div>
      </div>
      <div class="xtx-form-item">
        <div class="field">
          <i class="icon iconfont icon-phone"></i>
          <Field v-model="form.mobile" name="mobile" class="input" type="text" placeholder="请输入手机号" />
        </div>
        <div v-if="errors.mobile" class="error">{{ errors.mobile }}</div>
      </div>
      <div class="xtx-form-item">
        <div class="field">
          <i class="icon iconfont icon-code"></i>
          <Field v-model="form.code" name="code" class="input" type="text" placeholder="请输入验证码" />
          <span class="code" @click="send()">{{time === 0? '发送验证码': `${time}秒后发送`}}</span>
        </div>
        <div v-if="errors.code" class="error">{{ errors.code }}</div>
      </div>
      <div class="xtx-form-item">
        <div class="field">
          <i class="icon iconfont icon-lock"></i>
          <Field v-model="form.password" name="password" class="input" type="password" placeholder="请输入密码" />
        </div>
        <div v-if="errors.password" class="error">{{ errors.password }}</div>
      </div>
      <div class="xtx-form-item">
        <div class="field">
          <i class="icon iconfont icon-lock"></i>
          <Field v-model="form.rePassword" name="rePassword" class="input" type="password" placeholder="请确认密码" />
        </div>
        <div v-if="errors.rePassword" class="error">{{ errors.rePassword }}</div>
      </div>
      <a href="javascript:;" class="submit" @click="submit()">立即提交</a>
    </Form>
  </template>
  
  <script>
import { reactive, ref, onUnmounted  } from 'vue'
import {Form, Field} from 'vee-validate'
import schema from '@/utills/vee-validate-schema'
import {userQQPatchCode} from '@/api/user'
import { useRouter } from 'vue-router'
import { useStore } from 'vuex'
import Message from '@/components/library/xtx-message.js'
import { useIntervalFn } from '@vueuse/core'
import {userQQPatchLogin} from '@/api/user'
  export default {
    name: 'CallbackPatch',
    components: {Form, Field},
    props: {
      unionId: {
        type: String,
        default: ''
      }
    },
    setup(props) {
      const formCom = ref(null)
      //校验规则：判断用户名是否存在，确认密码校验规则
      //发送验证码
      //校验整体表单，提交


      //数据对象,双向绑定
      const form = reactive({
        account: null,
        mobile: null,
        code: null,
        password: null,
        rePassword: null
      })

      //校验对象
      const mySchema = {
        account: schema.accountApi,
        mobile: schema.mobile,
        code: schema.code,
        password: schema.password,
        rePassword: schema.rePassword
      }

      //发送验证码
     
      //倒计时
      const time = ref(0)
      //pause暂停, resume开始
      const {pause, resume} = useIntervalFn(() => {
        time.value--
        if(time.value <= 0) {
          time.value = 0
          pause()
        }
      }, 1000, false)


      onUnmounted(() => {
            pause()
        })

      const send = async () => {

        //手机号校验通过后，发送请求
        const vaild = mySchema.mobile(form.mobile)
       
        if(vaild === true) {
          if(time.value === 0) {
            await userQQPatchCode(form.mobile)
            Message({type: 'success', text: '发送成功'})
            time.value = 60
            resume()
          }
        } else {
            formCom.value.setFieldError('mobile', vaild)
          }
      }

      //提交
      const router = useRouter()
      const store = useStore()
      const submit = async () => {
      const valid = formCom.value.validate()
      if (valid) {
        userQQPatchLogin({
          unionId: props.unionId,
          ...form
        }).then(data => {
          // 实现和之前登录一样的逻辑
          // 1. 存储用户信息
          const { id, account, avatar, mobile, nickname, token } = data.result
          store.commit('user/setUser', { id, account, avatar, mobile, nickname, token })
          store.dispatch('cart/mergeCart').then(() => {
            // 2. 跳转到来源页或者首页
            router.push(store.state.user.redirectUrl)
            // 3. 成功提示
            Message({ type: 'success', text: 'QQ完善信息成功' })
          })
        }).catch(e => {
          Message({ type: 'error', text: '完善信息失败' })
        })
      }
    }




      return {form, mySchema, send, time, submit}
    }
  }
  </script>
  
  <style scoped lang='less'>
  .code {
    position: absolute;
    right: 0;
    top: 0;
    line-height: 50px;
    width: 80px;
    color: #999;
    &:hover {
      cursor: pointer;
    }
  }
  </style>
  